<?php
/**
 * 用户留言管理
 */
class MessageApp extends CommonApp {
	
	private $table = 'message';

	/**
	 * 首页
	 */
	public function indexAct(){
		//search
		$search = array();
		$search['where'] = "A.item != 'reply' ";
		$search['page'] = My::arr2var($_REQUEST,'page').(isset($_SESSION['pagesize'])?$_SESSION['pagesize']:'');//用于参与缓存标识	
		$search['join'] = "#__message as A ";
        $search['join'] .= "LEFT JOIN #__member as B ON A.member_id=B.id ";
        $search['field'] = "A.*,B.username,B.nickname";
        $search['order'] = "A.id DESC";
		//搜索标题
		if(My::arr2var($_GET,'title')!=''){
			$title = I('get.title');
			$search['where'] .= "AND A.`title` like '%{$title}%' ";
		}
		//搜索内容
		if(My::arr2var($_GET,'content')!=''){
			$content = I('get.content');
			$search['where'] .= "AND A.`content` like '%{$content}%' ";
		}
		//搜索留言者
		if(My::arr2var($_GET,'username')!=''){
			$username = I('get.username');
			$search['join'] = "#__message as A INNER JOIN #__member as B ON A.member_id=B.id AND (B.username like '%{$username}%' OR B.nickname like '%{$username}%')";
		}
		//搜索手机
		if(My::arr2var($_GET,'tel')!=''){
			$tel = I('get.tel');
			$search['where'] .= "AND A.`tel` like '%{$tel}%' ";
		}
		//搜索邮箱
		if(My::arr2var($_GET,'email')!=''){
			$email = I('get.email');
			$search['where'] .= "AND A.`email` like '%{$email}%' ";
		}
		//搜索id
		if(My::is_id(My::arr2var($_GET,'id'))){
			$id = $_GET['id'];
			$search['where'] .= "AND A.`id`='{$id}' ";
		}
		//搜索留言类型
		if(My::arr2var($_GET,'type')!=''){
			$item = I('get.item');
			$search['where'] .= "AND A.`item`='{$item}' ";
		}

		$result=My::getCache($search,array('pre'=>$this->table));
		if($result==''){
			//page
			$total_sql = DB('')->table($search['join'])->field('1')->where($search['where'])->group('A.id')->select(false);
			$result['page'] = My::page(My::sql2count($total_sql));
			//result
			$result['data'] = DB('')->field($search['field'])->join($search['join'])->where($search['where'])->group('A.id')->order($search['order'])->limit($result['page']['limits'])->select();
			if(is_array($result['data'])){
			    foreach($result['data'] as &$value){
			        $value['params'] = My::decodeJson($value['params']);
			        $value['reply'] = DB('message')->where("item='reply' AND idid='{$value['id']}'")->order('id DESC')->cache()->find();
			    }
			}
			// echo DB('')->getLastSql();
			My::setCache($search,$result,array('pre'=>$this->table));
		}
		$this->assign('result',$result);
		//print_r($result);
		//display
		$this->display("Message/index");
	}
	
	
	/**
	 * 回复
	 * @params $_REQUEST['id']
	 */
	public function replyAct(){
	    $idid = My::arr2var($_REQUEST,'idid');
	    if(!My::is_id($idid))
	        $this->ajaxReturn(404, 'ID无效!');
        if($_POST){
            if(!token())
                $this->ajaxReturn(300,'令牌验证错误!');
            if(empty($_POST['content']))
                $this->ajaxReturn(300,'回复内容不能为空!');
            $data['content'] = My::arr2var($_POST,'content');
            $data['idid'] = $idid;
            $data['item'] = 'reply';
            $data['adddate'] = date("Y-m-d H:i:s");
            $data['ip'] = get_client_ip();
            $data['author'] = My::user('username');
            $ok = DB('message') ->clear()->add($data);
            if($ok){
                self::sendEmail($data);
                $this->ajaxReturn(200,"回复成功");
            }else
                $this->ajaxReturn(300,"回复失败");
        }
        $this->display();
	}
	
	/**
	 * 发邮件
	 */
	private function sendEmail($data){
	    $emailParam['title'] = '留言回复';
        $emailParam['content'] = str_replace("\r\n","<br>",$data['content']);
        $oMessage = My::id2detail('message',$data['idid']);
        if(!$oMessage or $oMessage<0)
            return false;
        if(!My::is_email($oMessage['email']))
            return false;
        $oContent = str_replace("\r\n","<br>",$oMessage['content']);
        $emailParam['content'] .= "<br>------------------ 原始留言 ------------------<br><div style='font-size: 12px; background: #efefef; padding: 8px;'>{$oContent}</div>";
        $emailParam['receiver'] = array($oMessage['email']);
        $sendok = My::sendMail($emailParam);
        if($sendok==true)
            return true;
        else
            return false;
	}
}